﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace WebLib
{
    public class QueryStringHelper
    {
        public static string GetString(string name)
        {
            return GetString(name, null);
        }

        public static string GetString(string name, string defaultValue)
        {
            var returnValue = HttpContext.Current.Request.QueryString[name];

            if (defaultValue != null && returnValue == null)
                returnValue = defaultValue;

            return returnValue;
        }

        public static int GetInt(string name)
        {
            return GetInt(name, 0);
        }

        public static int GetInt(string name, int defaultValue)
        {
            var returnNumber = 0;

            var value = HttpContext.Current.Request.QueryString[name];

            var isNumber = int.TryParse(value, out returnNumber);

            if (defaultValue != 0 && returnNumber == 0)
                returnNumber = defaultValue;

            return returnNumber;
        }

        public static int GetByte(string name)
        {
            return GetByte(name, 0);
        }

        public static byte GetByte(string name, byte defaultValue)
        {
            byte returnByte = 0;

            var value = HttpContext.Current.Request.QueryString[name];

            var isNumber = byte.TryParse(value, out returnByte);

            if (defaultValue != 0 && returnByte == 0)
                returnByte = defaultValue;

            return returnByte;
        }

        public static bool GetBoolean(string name)
        {
            string value = HttpContext.Current.Request.QueryString[name];

            if (string.IsNullOrEmpty(value))
                return false;
            
            return value == "1" || value.ToLower() == "true" || value.ToLower() == "on";
        }

        public static DateTime GetDate(string name)
        {
            return GetDate(name, DateTime.MinValue);
        }

        public static DateTime GetDate(string name, DateTime defaultValue)
        {
            var returnDateTime = DateTime.MinValue;

            var value = HttpContext.Current.Request.QueryString[name];

            var isDateTime = DateTime.TryParse(value, out returnDateTime);

            if (defaultValue != DateTime.MinValue && returnDateTime == DateTime.MinValue)
                returnDateTime = defaultValue;

            return returnDateTime;
        }

        public static string[] GetStringArray(string name, char split, string[] defaultValue)
        {
            var value = HttpContext.Current.Request.QueryString[name];

            return !string.IsNullOrWhiteSpace(value) ? value.Split(split) : defaultValue;
        }

        public static int[] GetIntArray(string name, char split, int[] defaultValue)
        {
            var value = HttpContext.Current.Request.QueryString[name];

            if (!string.IsNullOrWhiteSpace(value))
            {
                string[] arrValue = value.Split(split);
                int[] arrIntValue = new int[arrValue.Length];

                for (var i = 0; i < arrValue.Length; i++)
                {
                    int returnNumber;
                    if(int.TryParse(arrValue[i], out returnNumber))
                        arrIntValue[i] = returnNumber;
                }

                return arrIntValue;
            }
            
            return defaultValue;
        }
    }
}
